package com.detrum.detrumdx.fragment;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.detrum.base.BaseStatic;
import com.detrum.detrumdx.R;
import com.detrum.detrumdx.adapter.WifiInfoAdapter;
import com.detrum.detrumdx.base.DeviceStatic;
import com.detrum.wifidevice.bean.WifiInfoBean;
import com.google.gson.Gson;

import java.util.List;

/**
 * Created by abc on 2018/2/2.
 */

public class SelectPopupwindow {
    private PopupWindow popupWindow;
    private TextView select_title;
    private ListView selectList;
    // TODO: 2018/5/24 蓝牙相关
    //private List<ScanResult> resultList;
    private List<WifiInfoBean.DataBean> beanList;
    public interface PopupwindowCall{
        // TODO: 2018/5/24 蓝牙相关
        //void onItem(ScanResult scanResult);
        void onItem(WifiInfoBean.DataBean wifiBean);
        void dismiss();
    }
    public void init(Context context, String json , String name, View parentView,final PopupwindowCall popupwindowCall){
        View view = View.inflate(context, R.layout.fragment_select,null);
        select_title = view.findViewById(R.id.select_title);
        selectList = view.findViewById(R.id.selectList);
        if(name.equals(DeviceStatic.Bluetooth)){
            select_title.setText(R.string.select_device_bluetooth);
            Gson gson = new Gson();
            if(json!=null&&json.length()!=0){
                // TODO: 2018/5/24 蓝牙相关
               /* BluetoothInfoBean bluetoothInfoBean = gson.fromJson(json,BluetoothInfoBean.class);
                if(resultList!=null){
                    resultList.clear();
                    resultList = null;
                }
                resultList = bluetoothInfoBean.getResultList();
                BluetoothInfoAdapter adapter = new BluetoothInfoAdapter(context,resultList);
                selectList.setAdapter(adapter);
                selectList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        popupwindowCall.onItem(resultList.get(position));
                        popupWindow.dismiss();
                    }
                });*/
            }
        }else if(name.equals(DeviceStatic.Wifi)){
            select_title.setText(R.string.select_device_wifi);
            Gson gson = new Gson();
            if(json!=null&&json.length()!=0){
                WifiInfoBean wifiInfoBean = gson.fromJson(json,WifiInfoBean.class);
                if(beanList!=null){
                    beanList.clear();
                    beanList=null;
                }
                beanList = wifiInfoBean.getBeanList();
                WifiInfoAdapter adapter = new WifiInfoAdapter(context,beanList);
                selectList.setAdapter(adapter);
                selectList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        popupwindowCall.onItem(beanList.get(position));
                        popupWindow.dismiss();
                    }
                });
            }
        }
        int width = BaseStatic.SCREEN_WIDTH / 2;
        int height = BaseStatic.SCREEN_HEIGHT / 2;
        int topMargin = BaseStatic.SCREEN_HEIGHT / 4;
        int leftMargin = BaseStatic.SCREEN_WIDTH / 4;
        popupWindow = new PopupWindow(view,width,height,true);
        ColorDrawable drawable = new ColorDrawable(context.getResources().getColor(R.color.bg_color));
        popupWindow.setBackgroundDrawable(drawable);
        popupWindow.showAtLocation(parentView, Gravity.CENTER,0,0);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                popupwindowCall.dismiss();
            }
        });
    }
}
